// pages/register/register.js
const inputMixin = require('../../utils/mixins/input')
const jobMixin = require('../../utils/mixins/job')
const loading = require('../../utils/tools/loading')
const { category, values } = require('../../utils/const/job')
const iconBasicPath = '/static/images/register/icons'
const tabItems = [
  {
    text: '个人注册',
    iconPath: `${iconBasicPath}/personal.png`,
    activedIconPath: `${iconBasicPath}/personal-active.png`,
  },
  {
    text: '企业注册',
    iconPath: `${iconBasicPath}/industry.png`,
    activedIconPath: `${iconBasicPath}/industry-active.png`,
  },
]

Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabItems,
    tabIndex: 0,
    errorMsg: '',

    // 个人注册
    belongs: ['1公司', '2公司', '3公司'],
    belong: '',
    jobs: [category, values[0]],
    job: '',

    // 企业注册
    company: '',
    address: '',
    tax: '',

    // 通用属性
    name: '',
    mobile: '',
    code: '',
    password: '',
    repassword: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  /**
   * @desc 当tab切换时
   */
  onTabChange(e){
    let {currentTarget: {dataset: {index}}} = e

    this.setData({tabIndex: index})
  },

  /**
   * @desc 当下拉选择切换时
   */
  onPickerChange(e){
    let data = {}
    let {
      currentTarget: {dataset: {pickerType, pickerRangeName}},
      detail: { value: index }
    } = e

    data[pickerType] = this.data[pickerRangeName][index]
    this.setData(data)
  },

  /**
   * @desc 注册
   */
  register() {
    if (loading.isLoading) return
    let errorMsg = this.validate(this.getValidateOptions())
    if (errorMsg) {
      return this.setErrorMsg(errorMsg)
    }
    this.clearErrorMsg()

    loading.show('请稍后')

    setTimeout(function () {
      loading.hide()

      wx.navigateTo({
        url: '/pages/login/login',
      })
    }, 1000)
  },

  /**
   * @desc 获取验证条件
   */
  getValidateOptions() {
    let { belong, job, name, mobile, code, password, repassword, company, address, tax, tabIndex } = this.data
    let range = tabIndex === 0 ? [0, -3] : [2] // 范围
    let options = [
      {
        value: belong,
        strategies: [
          { strategy: 'isNotEmpty', errorMsg: '所属公司不能为空' },
        ],
      }, 
      {
        value: job,
        strategies: [
          { strategy: 'isNotEmpty', errorMsg: '所在岗位不能为空' },
        ],
      },
      {
        value: name,
        strategies: [
          { strategy: 'isNotEmpty', errorMsg: '姓名不能为空' },
        ],
      },
      {
        value: mobile,
        strategies: [
          { strategy: 'isNotEmpty', errorMsg: '手机号码不能为空' },
          { strategy: 'isPhone', errorMsg: '手机号码格式不对' },
        ],
      },
      {
        value: code,
        strategies: [
          { strategy: 'isNotEmpty', errorMsg: '验证码不能为空' },
        ],
      },
      {
        value: password,
        strategies: [
          { strategy: 'isNotEmpty', errorMsg: '密码不能为空' },
        ],
      },
      {
        value: repassword,
        strategies: [
          { strategy: 'isNotEmpty', errorMsg: '确认密码不能为空' },
          { strategy: `equalTo:${password}`, errorMsg: '确认密码与密码不同' },
        ],
      },
      {
        value: company,
        strategies: [
          { strategy: 'isNotEmpty', errorMsg: '公司不能为空' },
        ],
      }, 
      {
        value: address,
        strategies: [
          { strategy: 'isNotEmpty', errorMsg: '公司地址不能为空' },
        ],
      }, 
      {
        value: tax,
        strategies: [
          { strategy: 'isNotEmpty', errorMsg: '纳税人识别号不能为空' },
        ],
      }, 
    ]

    return options.slice(...range)
  },

  ...inputMixin,
  ...jobMixin,
})